<?php
class ModelCommunityCardManage extends Model {
public function getTotalCardManages() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "config_card_manage`");
		return $query->row['total'];
	}
	
	private function filterSql($data){
		$sql ="";
		
		if(isset($data['filter_title'])&&!empty($data['filter_title'])){
			$sql .= " and title like '%".$data['filter_title']."%'";
		}
		
		if(isset($data['filter_news_id'])&&!empty($data['filter_news_id'])){
			$sql .= " and news_id='".(int)$data['filter_news_id']."'";
		}
		
		return $sql;
		
	}
	
	private function filterPoliceSql($data){
		$sql ="";
		
		if(isset($data['filter_card_manage_id'])&&!empty($data['filter_card_manage_id'])){
			$sql .= " and card_manage_id='".(int)$data['filter_card_manage_id']."'";
		}
		
		return $sql;
		
	}
	
	private function filterAssistantPoliceSql($data){
		$sql ="";
		
		if(isset($data['filter_card_manage_id'])&&!empty($data['filter_card_manage_id'])){
			$sql .= " and card_manage_id='".(int)$data['filter_card_manage_id']."'";
		}
		
		return $sql;
		
	}
	
	
	
	public function getRelatedPolices($data=array()){
		$sql ="select * from ".DB_PREFIX."config_police cp left join ".DB_PREFIX."config_police_to_card_manage cptcm on(cp.police_id=cptcm.police_id) where 1=1 ";
		$sql .= $this->filterPoliceSql($data);
		return $this->db->query($sql)->rows;
	}
	
	public function getRelatedAssistantPolices($data=array()){
		$sql ="select * from ".DB_PREFIX."config_assistant_police cp left join ".DB_PREFIX."config_assistant_police_to_card_manage cptcm on(cp.assistant_police_id=cptcm.assistant_police_id) where 1=1 ";
		$sql .= $this->filterAssistantPoliceSql($data);
		return $this->db->query($sql)->rows;
	}
	
	public function getAllCardManages($community_id){
		$sql = "SELECT * FROM `" . DB_PREFIX . "config_card_manage`  where community_id='".(int)$community_id."'";
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
	public function getCardManages($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "config_card_manage`  where 1=1";

		$sql .=$this->filterSql($data);
		
		$sort_data = array(
			'date_added'
		);	
			
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
	
	public function getCardManage($card_manage_id){
		$sql = "SELECT * FROM `" . DB_PREFIX . "config_card_manage`  where card_manage_id='".(int)$card_manage_id."'";
		return $this->db->query($sql)->row;
	}
	
	public function addCardManage($data= array()){
		$sql = "insert into ".DB_PREFIX."config_card_manage set police_office='".$data['police_office']."',address='".$data['address']."',police_station='".$data['police_station']."',so='".$data['so']."',dianhua='".$data['dianhua']."',community_id='".(int)$data['community_id']."'" ;
		$this->db->query($sql);
		
		$lastId = $this->db->getLastId();
		if(isset($data['police_route'])){
				foreach ($data['police_route'] as $police) {
					$this->db->query("insert into `".DB_PREFIX."config_police_to_card_manage` set police_id='".(int)$police."',card_manage_id='".(int)$lastId."'");
				}
		}
		
		if(isset($data['assistant_police_route'])){
				foreach ($data['assistant_police_route'] as $police) {
					$this->db->query("insert into `".DB_PREFIX."config_assistant_police_to_card_manage` set assistant_police_id='".(int)$police."',card_manage_id='".(int)$lastId."'");
				}
		}
	}
	
	public function editCardManage($card_manage_id,$data=array()){
		$sql ="update ".DB_PREFIX."config_card_manage set police_office='".$data['police_office']."',address='".$data['address']."',police_station='".$data['police_station']."',so='".$data['so']."',dianhua='".$data['dianhua']."',community_id='".(int)$data['community_id']."' where card_manage_id='".(int)$card_manage_id."'";
		$this->db->query($sql);
		
		$this->db->query("delete from `".DB_PREFIX."config_police_to_card_manage`  where card_manage_id='".(int)$card_manage_id."'");
		if(isset($data['police_route'])){
				foreach ($data['police_route'] as $police) {
					$this->db->query("insert into `".DB_PREFIX."config_police_to_card_manage` set police_id='".(int)$police."',card_manage_id='".(int)$card_manage_id."'");
				}
		}
		
	
		$this->db->query("delete from `".DB_PREFIX."config_assistant_police_to_card_manage`  where card_manage_id='".(int)$card_manage_id."'");
		if(isset($data['assistant_police_route'])){
				foreach ($data['assistant_police_route'] as $police) {
					$this->db->query("insert into `".DB_PREFIX."config_assistant_police_to_card_manage` set assistant_police_id='".(int)$police."',card_manage_id='".(int)$card_manage_id."'");
				}
		}
	}
	
	public function deleteCardManage($card_manage_id){
		$sql ="delete  from ".DB_PREFIX."config_card_manage where card_manage_id='".(int)$card_manage_id."'";
		$this->db->query($sql);
	}
}
?>